<script setup lang="ts">
import { ref, watch } from 'vue'
import { useFetchCompanies } from '../composables/useFetchCompanies'
import { baseUrl } from '../config'

const emit = defineEmits(['select', 'create', 'edit', 'delete'])
const { companies, loading, error } = useFetchCompanies(baseUrl)

const innerSelectedId = ref<number | null>(null)
const showActionMenuId = ref<number | null>(null)

watch(
  companies,
  (list) => {
    // 如果公司列表为空且没有选中公司，则清空选中状态
    if(list.length === 0) {
      innerSelectedId.value = null
      return
    }
    // 如果没有选中公司且列表不为空，默认选中第一个公司
    if (!innerSelectedId.value || !list.some(c => c.id === innerSelectedId.value)) {
      innerSelectedId.value = list[0].id
      emit('select', list[0])
    }
  },
  { immediate: true }
)

function handleCompanyClick(company: { id: number; name: string }) {
  innerSelectedId.value = company.id
  emit('select', company)
}

function openModify(company: { id: number; name: string; comment: string }) {
  emit('edit', company)
}
function askDelete(company: { id: number; name: string }) {
  emit('delete', company)
}
function toggleActionMenu(companyId: number) {
  showActionMenuId.value = showActionMenuId.value === companyId ? null : companyId
}
function closeActionMenu() {
  showActionMenuId.value = null
}
</script>

<template>
  <div class="company-sidebar">
    <button class="create-company-btn" @click="emit('create')">+ 创建组织</button>
    <div class="sidebar-title">组织列表</div>
    <div class="company-list-scroll">
      <ul class="company-list">
        <li
          v-if="loading"
          class="company-item"
        >加载中...</li>
        <li
          v-else-if="error"
          class="company-item"
        >{{ error }}</li>
        <li
          v-else
          v-for="company in companies"
          :key="company.id"
          class="company-item"
          :class="{ selected: company.id === innerSelectedId }"
          @click="handleCompanyClick(company)"
          :title="company.comment"
        >
          <span>{{ company.name }}</span>
          <div class="item-actions" @click.stop>
            <button class="action-menu-btn" @click="toggleActionMenu(company.id)">
              ⋮
            </button>
            <div
              v-if="showActionMenuId === company.id"
              class="action-menu"
              @mousedown.prevent
              @wheel.stop
              @click.stop
            >
              <div class="action-menu-item" @click="openModify(company); closeActionMenu()">编辑</div>
              <div class="action-menu-item danger" @click="askDelete(company); closeActionMenu()">删除</div>
            </div>
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>

<style scoped>
.company-sidebar {
  width: 220px;
  min-height: 100vh;
  background: #f5f7fa;
  border-right: 1px solid #e6e6e6;
  padding-top: 24px;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
}
.create-company-btn {
  display: block;
  width: 90%;
  margin: 0 auto 18px auto;
  padding: 8px 0;
  background: #1890ff;
  color: #fff;
  border: none;
  border-radius: 6px;
  font-size: 15px;
  cursor: pointer;
  transition: background 0.2s;
}
.create-company-btn:hover {
  background: #40a9ff;
}
.sidebar-title {
  font-size: 18px;
  font-weight: bold;
  padding: 0 24px 12px 24px;
  color: #333;
}
.company-list-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
}
.company-list {
  padding: 0;
  margin: 0;
  list-style: none;
}
.company-item {
  transition: background 0.2s, color 0.2s;
  font-size: 16px;
  padding: 12px 24px;
  border-radius: 0 20px 20px 0;
  margin: 0;
  cursor: pointer;
  color: #333;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: relative;
}
.company-item.selected,
.company-item:hover {
  background: #e6f7ff;
  color: #1890ff;
}
.item-actions {
  position: relative;
  display: flex;
  align-items: center;
  z-index: 2;
}
.action-menu-btn {
  background: transparent;
  border: none;
  font-size: 18px;
  cursor: pointer;
  padding: 2px 8px;
  border-radius: 4px;
  color: #888;
  transition: background 0.2s;
}
.action-menu-btn:hover {
  background: #e6f7ff;
  color: #1890ff;
}
.action-menu {
  position: absolute;
  top: 28px;
  right: 0;
  min-width: 80px;
  background: #fff;
  border: 1px solid #eee;
  border-radius: 6px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
  z-index: 100;
  display: flex;
  flex-direction: column;
}
.action-menu-item {
  padding: 8px 16px;
  font-size: 14px;
  color: #333;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.2s, color 0.2s;
}
.action-menu-item:hover {
  background: #f5f5f5;
  color: #1890ff;
}
.action-menu-item.danger {
  color: #ff4d4f;
}
.action-menu-item.danger:hover {
  background: #fff0f0;
  color: #ff4d4f;
}
.error {
  color: #ff4d4f;
  margin-bottom: 8px;
}
</style>